package com.lzh.mybatisdemo.controller;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author by lzh
 * @Classname NumberConveter
 * @Description TODO
 * @Date 2024/12/26 10:57
 */
public class DoubleConverter implements Converter<Double> {

    @Override
    public Class<Double> supportJavaTypeKey() {
        return Double.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }

    @Override
    public WriteCellData<?> convertToExcelData(Double value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        double doubleValue = BigDecimal.valueOf(value).setScale(5, RoundingMode.HALF_UP).doubleValue();
        return NumberUtils.formatToCellData(doubleValue, contentProperty);

    }


}
